home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / admin / xinetd.2 / xinetd / xinetd.2.1.7-linux.4 / libs / src / pset / pset.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-25  |  2.2 KB  |  127 lines

  1. /*
  2.  * (c) Copyright 1992, 1993 by Panagiotis Tsirigotis
  3.  * All rights reserved.  The file named COPYRIGHT specifies the terms 
  4.  * and conditions for redistribution.
  5.  */
  6.  
  7. static char RCSid[] = "$Id: pset.c,v 3.1 1993/03/06 18:48:57 panos Exp $" ;
  8. static char version[] = VERSION ;
  9.  
  10. #include "pset.h"
  11.  
  12. #ifndef NULL
  13. #define NULL                        0
  14. #endif
  15.  
  16. #define ALLOC_START                20
  17. #define ALLOC_STEP                10
  18.  
  19. #define POINTER                    __pset_pointer
  20.  
  21. char *malloc(), *realloc() ;
  22.  
  23.  
  24. /*
  25.  * Create a pointer set and return a handle to it.
  26.  * Some space is initially allocated for the set.
  27.  */
  28. pset_h pset_create( alloc_start, alloc_step )
  29.     unsigned alloc_start, alloc_step ;
  30. {
  31.     pset_h pset ;
  32.     unsigned start ;
  33.  
  34.     pset = (pset_h) malloc( sizeof( struct __pset ) ) ;
  35.     if ( pset == NULL )
  36.         return( NULL ) ;
  37.     
  38.     start = ( alloc_start == 0 ) ? ALLOC_START : alloc_start ;
  39.     pset->ptrs = (POINTER *) malloc( start * sizeof( POINTER ) ) ;
  40.     if ( pset->ptrs == NULL )
  41.     {
  42.         free( (char *) pset ) ;
  43.         return( NULL ) ;
  44.     }
  45.  
  46.     pset->max = start ;
  47.     pset->count = 0 ;
  48.     pset->alloc_step = ( alloc_step == 0 ) ? ALLOC_STEP : alloc_step ;
  49.     return( pset ) ;
  50. }
  51.  
  52.  
  53. /*
  54.  * Destroy a pset
  55.  */
  56. void pset_destroy( pset )
  57.     pset_h pset ;
  58. {
  59.     free( (char *) pset->ptrs ) ;
  60.     free( (char *) pset ) ;
  61. }
  62.  
  63.  
  64. /*
  65.  * Insert a pointer to a pset
  66.  */
  67. POINTER pset_insert( pset, p )
  68.     pset_h pset ;
  69.     POINTER p ;
  70. {
  71.     if ( pset->count >= pset->max )
  72.     {
  73.         unsigned new_max = pset->max + pset->alloc_step ;
  74.         POINTER *new_ptrs ;
  75.  
  76.         new_ptrs = (POINTER *) realloc(
  77.                                 (char *)pset->ptrs, new_max * sizeof( POINTER ) ) ;
  78.         if ( new_ptrs == NULL )
  79.             return( NULL ) ;
  80.         pset->max = new_max ;
  81.         pset->ptrs = new_ptrs ;
  82.     }
  83.     return( pset->ptrs[ pset->count++ ] = p ) ;
  84. }
  85.  
  86.  
  87. /*
  88.  * Remove a pointer from a pset
  89.  */
  90. void pset_delete( pset, ptr )
  91.     register pset_h pset ;
  92.     register POINTER ptr ;
  93. {
  94.     register unsigned u = pset->count ;
  95.  
  96.     if ( u == 0 )
  97.         return ;
  98.     
  99.     do
  100.     {
  101.         u-- ;
  102.         if ( pset->ptrs[ u ] == ptr )
  103.         {
  104.             pset->ptrs[ u ] = pset->ptrs[ --pset->count ] ;
  105.             return ;
  106.         }
  107.     }
  108.     while ( u ) ;
  109. }
  110.  
  111.  
  112. /*
  113.  * Create a pset iterator
  114.  */
  115. psi_h psi_create( pset )
  116.     pset_h pset ;
  117. {
  118.     psi_h iter = (psi_h) malloc( sizeof( struct __pset_iterator ) ) ;
  119.  
  120.     if ( iter == NULL )
  121.         return( NULL ) ;
  122.     iter->pset = pset ;
  123.     return( iter ) ;
  124. }
  125.  
  126.  
  127.